﻿var x0 = 0, y0 = 0, x1 = 0, y1 = 0;
var offx = 6, offy = 6;
var moveable = false;
var hover = 'orange', normal = 'slategray'; //color;
var index = 10000; //z-index;
var xx;
var NODE_START = '100201';
var NODE_END = '100202';
var NODE_MANUAL = '100203';
var NODE_DECISION = '100204';
var NODE_COALITION = '100205';
var NODE_LINE = '1';
var NODE_BLINE = '2';
var SELECTTOOLTYPE = '';
function initialize() {
    var tb = "<table width='100%' cellpadding='5px' style='text-align:center'>";
//    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_START + ");'><img alt='开始节点' src='../Images/Start.ico' /></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_MANUAL + ");'><img alt='人工处理节点' src='../Images/Manual.ico'/></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_DECISION + ");'><img alt='决策节点' src='../Images/Decision.ico' /></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_COALITION + ");'><img alt='聚合节点' src='../Images/Coalition.ico' /></td></tr>";
    //    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_END + ");'><img alt='结束节点' src='../Images/End.ico'   /></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_BLINE + ");'><img alt='曲线连接' src='../Images/PLine.ico' /></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='setCursor(" + NODE_LINE + ");'><img alt='直线连接' src='../Images/Line.ico' /></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='mEditFlow();'><img alt='流程属性' src='../Images/JobTemp.ico' /></td></tr>";
    tb += "<tr><td onmouseover=\"this.className='borderShow'\" onmouseout=\"this.className=''\" onclick='checkFlow();'><img alt='检查流程有效性' src='../Images/JobTemp_check.ico' /></td></tr>";
    tb += "</table>"

    var a = new xWin("1", 55, 400, document.body.offsetWidth - 70, 10, "工具条", tb);
}
function setCursor(type) {
    SELECTTOOLTYPE = type;
    if (type == NODE_LINE || type== NODE_BLINE) {
        document.body.style.cursor = "hcross.cur";
    }
    else {
        document.body.style.cursor = 'SELECT.CUR';
    }
}

function addFlowObj() {
    if (SELECTTOOLTYPE != '' && SELECTTOOLTYPE != NODE_LINE && SELECTTOOLTYPE != NODE_BLINE) {
        var xPos = (event.clientX + document.body.scrollLeft);
        var yPos = (event.clientY + document.body.scrollTop);
        var cObj = document.getElementById("FlowVML");
        var cSize = cObj.coordsize + '';
        var cdx = parseInt(cSize.slice(0, cSize.indexOf(',')));
        var cdy = parseInt(cSize.slice(cSize.indexOf(',') + 1, cSize.length));
        var xr = cdx / parseInt(cObj.style.width);
        var yr = cdy / parseInt(cObj.style.height);      
        var _left = parseInt(xPos * xr);
        var _top = parseInt(yPos * yr);
        $.ajax({
            type: "POST",
            url: "JSON/Flow.ashx",
            data: "ActionType=1&FlowID=" + FlowID + "&NodeType=" + SELECTTOOLTYPE + "&X=" + _left + "&Y=" + _top,
            success: function (msg) {
                if (msg == '1') {
                    loadFlow();
                }
            }
        });
        SELECTTOOLTYPE = '';
    }
}
function checkFlow() {
    $.ajax({
        type: "POST",
        url: "JSON/Flow.ashx",
        data: "ActionType=6&FlowID=" + FlowID ,
        success: function (msg) {
            alert(msg);
        }
    });
}
function clearCursor() {
    document.body.style.cursor = '';
    SELECTTOOLTYPE = '';
}
//开始拖动;
function startDrag(obj) {
    if (event.button == 1) {
        //锁定标题栏;
        obj.setCapture();
        //定义对象;
        var win = obj.parentNode;
        var sha = win.nextSibling;
        //记录鼠标和层位置;
        x0 = event.clientX;
        y0 = event.clientY;
        x1 = parseInt(win.style.left);
        y1 = parseInt(win.style.top);
        //记录颜色;
        normal = obj.style.backgroundColor;
        //改变风格;
        obj.style.backgroundColor = hover;
        win.style.borderColor = hover;
        obj.nextSibling.style.color = hover;
        sha.style.left = x1 + offx;
        sha.style.top = y1 + offy;
        moveable = true;
    }
}
//拖动;
function drag(obj) {
    if (moveable) {
        var win = obj.parentNode;
        var sha = win.nextSibling;
        win.style.left = x1 + event.clientX - x0;
        sha.style.left = parseInt(win.style.left) + offx;
        if ((y1 + event.clientY - y0) > 0 && (y1 + event.clientY - y0) < 600) {
            win.style.top = y1 + event.clientY - y0;
            sha.style.top = parseInt(win.style.top) + offy;
        }
    }
}
//停止拖动;
function stopDrag(obj) {
    if (moveable) {
        var win = obj.parentNode;
        var sha = win.nextSibling;
        var msg = obj.nextSibling;
        win.style.borderColor = normal;
        obj.style.backgroundColor = normal;
        msg.style.color = normal;
        sha.style.left = obj.parentNode.style.left;
        sha.style.top = obj.parentNode.style.top;
        obj.releaseCapture();
        moveable = false;
    }
}
//获得焦点;
function getFocus(obj) {
    if (obj.style.zIndex != index) {
        index = index + 2;
        var idx = index;
        obj.style.zIndex = idx;
        obj.nextSibling.style.zIndex = idx - 1;
    }
}
//最小化;
function min(obj) {
    var win = obj.parentNode.parentNode;
    var sha = win.nextSibling;
    var tit = obj.parentNode;
    var msg = tit.nextSibling;
    var flg = msg.style.display == "none";
    if (flg) {
        win.style.height = parseInt(msg.style.height) + parseInt(tit.style.height) + 2 * 2;
        sha.style.height = win.style.height;
        msg.style.display = "block";
        obj.innerHTML = "0";
    }
    else {
        win.style.height = parseInt(tit.style.height) + 2 * 2;
        sha.style.height = win.style.height;
        obj.innerHTML = "2";
        msg.style.display = "none";
    }
}
//关闭;
function cls(obj) {
    var win = obj.parentNode.parentNode.parentNode;
    //var sha = win.nextSibling;
    win.style.visibility = "hidden";
    //sha.style.visibility = "hidden";
}
//显示/隐藏;
function ShowHide() {
    if (xx != null)
        if (xx.style.visibility == "hidden")
            xx.style.visibility = "visible";
        else if (xx.style.visibility == "visible")
            xx.style.visibility = "hidden";
}
//创建一个对象;
function xWin(id, w, h, l, t, tit, msg) {
    index = index + 2;
    this.id = id;
    this.width = w;
    this.height = h;
    this.left = l;
    this.top = t;
    this.zIndex = index;
    this.title = tit;
    this.message = msg;
    this.obj = null;
    this.bulid = bulid;
    this.bulid();
    xx = document.getElementById('allx');
    xx.style.visibility = "visible";

}
//初始化;
function bulid() {
    var str = ""
                + "<div id='allx'><div id='xMsg'" + this.id + " "
                + "style='"
                + "z-index:" + this.zIndex + ";"
                + "width:" + this.width + ";"
                + "height:" + this.height + ";"
                + "left:" + this.left + ";"
                + "top:" + this.top + ";"
                + "background-color:" + normal + ";"
                + "color:" + normal + ";"
                + "font-size:12px;"
                + "font-family:Verdana;"
                + "position:absolute;"
                + "cursor:default;"
                + "border:1px solid " + normal + ";"
                + "' "
                + "onmousedown='getFocus(this)'>"
                        + "<div "
                        + "style='"
                        + "background-color:" + normal + ";"
                        + "width:" + (this.width - 2 * 2) + ";"
                        + "height:20;"
                        + "color:white;"
                        + "' "
                        + "onmousedown='startDrag(this)' "
                        + "onmouseup='stopDrag(this)' "
                        + "onmousemove='drag(this)' "
                        + "ondblclick='min(this.childNodes[1])'"
                        + ">"
                                + "<span style='width:" + (this.width - 2 * 14 - 4) + ";padding-left:3px;'>" + this.title + "</span>"
                                + "<span style='width:14;border-width:0px;color:white;font-family:webdings;' onclick='min(this)'>0</span>"
//                                + "<span style='width:14;border-width:0px;color:white;font-family:webdings;' onclick='cls(this)'>r</span>"
                        + "</div>"
                                + "<div style='"
                                + "width:100%;"
                                + "height:" + (this.height - 20 - 4) + ";"
                                + "background-color:#f5fafe;"
                                + "line-height:12px;"
                                + "word-break:break-all;"
                                + "padding:0px;"
                                + "'>" + this.message + "</div>"
                + "</div>"
                + "<div id='xshadow' style='"
                + "width:" + this.width + ";"
                + "height:" + this.height + ";"
                + "top:" + this.top + ";"
                + "left:" + this.left + ";"
                + "z-index:" + (this.zIndex - 1) + ";"
                + "position:absolute;"
                + "background-color:black;"
                + "'>by wildwind</div></div>";
    document.getElementById('msgbox').innerHTML = str;
}
/**
 * 获取鼠标在页面上的位置
 * @param ev		触发的事件
 * @return			x:鼠标在页面上的横向位置, y:鼠标在页面上的纵向位置
 */
function getMousePoint(ev) {
	// 定义鼠标在视窗中的位置
	var point = {
		x:0,
		y:0
	};
 
	// 如果浏览器支持 pageYOffset, 通过 pageXOffset 和 pageYOffset 获取页面和视窗之间的距离
	if(typeof window.pageYOffset != 'undefined') {
		point.x = window.pageXOffset;
		point.y = window.pageYOffset;
	}
	// 如果浏览器支持 compatMode, 并且指定了 DOCTYPE, 通过 documentElement 获取滚动距离作为页面和视窗间的距离
	// IE 中, 当页面指定 DOCTYPE, compatMode 的值是 CSS1Compat, 否则 compatMode 的值是 BackCompat
	else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
		point.x = document.documentElement.scrollLeft;
		point.y = document.documentElement.scrollTop;
	}
	// 如果浏览器支持 document.body, 可以通过 document.body 来获取滚动高度
	else if(typeof document.body != 'undefined') {
		point.x = document.body.scrollLeft;
		point.y = document.body.scrollTop;
	}
 
	// 加上鼠标在视窗中的位置
	point.x += ev.clientX;
	point.y += ev.clientY;
 
	// 返回鼠标在视窗中的位置
	return point;
}
function getPosition_Iframe() {
    var point = {
        x: 0,
        y: 0
    };
    var parentWindow = window.parent;
    var tmpLocation = window.location;
    var target = null;
    var left = 0;
    var top = 0;
    while (parentWindow != null && typeof (parentWindow) != 'undefined' && tmpLocation != parentWindow.location) {
        for (var x = 0; x < parentWindow.frames.length; x++) {
            if (tmpLocation == parentWindow.frames[x].location) {
                target = parentWindow.frames[x].frameElement;
                break;
            }
        }
        do {
            left += target.offsetLeft - target.scrollLeft || 0;
            top += target.offsetTop - target.scrollTop || 0;
            target = target.offsetParent;
        } while (target);
        tmpLocation = parentWindow.location;
        parentWindow = parentWindow.parent;
    }
    point.x = left + window.event.clientX;
    point.y = top + window.event.clientY;
    return point;
}  
